#!/usr/bin/env node
// -*- js -*-

var fs = require('fs'),
    cssmin = require('cssmin'),
    util = require('util'),
    arg = process.argv.slice(2);

function squeeze_out(css_in) {
    process.stdout.write(cssmin(css_in) );
}

var pattern = /.*?\.css/;

if (arg.length) {
    arg = arg[0].replace(/^(-)+/,'');
    switch(arg) {
        case 'h' : showHelp();
            break;
        case 'help' : showHelp();
            break;
        default :
            var walkDirWithFileAction = function (dir, action) {


                fs.readdir(dir, function (err, list) {
                    // Return the error if something went wrong
                    if (err)
                        return action(err);

                    // For every file in the list
                    list.forEach(function (file) {
                        // Full path of that file
                        var path = dir + "/" + file;
                        // Get the file's stats
                        fs.stat(path, function (err, stat) {
                            //console.log(stat);
                            // If the file is a directory
                            if (stat && stat.isDirectory())
                            // Dive into the directory
                                walkDirWithFileAction(path, action);
                            else if(pattern.test(file))
                            // Call the action
                                action(null, path);
                        });
                    });
                });
            };

            walkDirWithFileAction(__dirname +  "/" +arg, function (error, path) {
                fs.readFile( path, "utf8", function(err, css_in) {

                    if (err) {
                        util.puts("Invalid file : "+ arg);
                        process.exit(1);
                    }
                    else {
                        squeeze_out(css_in);
                    }
                } );
            });
    }
}
else {
    var stdin = process.openStdin();
    stdin.setEncoding("utf8");
    var css_in = "";
    stdin.on("end", function() { squeeze_out(css_in) });
    stdin.on("data", function(chunk) { css_in += chunk });
}

function showHelp() {
    util.puts("Usage: node ./bin/cssmin [options] [<file>]");
    util.puts("\nIf no file is specified, assume stdin for input.");
    util.puts("\nOptions:");
    util.puts("\t--help, -h  Print usage info");
}